package com.shanzmoo.core.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.base.exception.OutletOrderException;
import com.shanzmoo.core.cache.CurrentUserCache;
import com.shanzmoo.db.personnel.entity.RootGroupEntity;
import com.shanzmoo.db.personnel.entity.dto.RootGroupQueryDto;
import com.shanzmoo.db.personnel.entity.dto.RootGroupSaveDto;
import com.shanzmoo.db.personnel.entity.dto.RootGroupUpdateDto;
import com.shanzmoo.db.personnel.entity.vo.RootGroupPageVo;
import com.shanzmoo.db.personnel.entity.vo.RootGroupSelectVo;
import com.shanzmoo.db.personnel.service.IRootGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 *
 * @author： Gzhao 2021/1/31
 */
@Slf4j
@Component
public class RootGroupManager {

    @Autowired
    private IRootGroupService rootGroupService;

    public SResponse<Boolean> add(RootGroupSaveDto saveDto) {
        // 检验组名是否重复
        checkGroupName(saveDto.getName());
        RootGroupEntity save = RootGroupSaveDto.convert(saveDto);
        save.setCreateUser(CurrentUserCache.getOutletId());

        rootGroupService.save(save);
        return SResponse.ok(Boolean.TRUE);
    }

    public SResponse<Boolean> edit(RootGroupUpdateDto updateDto) {
        RootGroupEntity old = rootGroupService.getById(updateDto.getId());
        if (!StrUtil.equals(old.getName(), updateDto.getName())){
            checkGroupName(updateDto.getName());
        }

        rootGroupService.updateById(RootGroupUpdateDto.convert(updateDto));
        return SResponse.ok(Boolean.TRUE);
    }

    public SResponse<PageRespBase<RootGroupPageVo>> getPageList(RootGroupQueryDto queryDto) {
        // 开启分页
        Page page = PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        List<RootGroupEntity> list = rootGroupService.list(
                new QueryWrapper<RootGroupEntity>()
                        .eq("deleted", false)
                        .like(StrUtil.isNotBlank(queryDto.getName()), "name", queryDto.getName())
                        .like(StrUtil.isNotBlank(queryDto.getGroupLeader()), "group_leader", queryDto.getGroupLeader())
                        .like(StrUtil.isNotBlank(queryDto.getMobile()), "mobile", queryDto.getMobile())
                        .eq(queryDto.getStatus() != null, "`status`", queryDto.getStatus())
                        .orderByAsc("id")
        );

        List<RootGroupPageVo> reList = list.stream().map(e -> {
            return RootGroupPageVo.create(e);
        }).collect(Collectors.toList());

        PageRespBase<RootGroupPageVo> re = PageRespBase.create(page, reList);
        PageHelper.clearPage();
        return SResponse.ok(re);
    }

    public SResponse<Boolean> disableById(Integer id) {
        RootGroupEntity rootGroupEntity = new RootGroupEntity();
        rootGroupEntity.setId(id);
        rootGroupEntity.setStatus(false);
        rootGroupService.updateById(rootGroupEntity);
        return SResponse.ok(Boolean.TRUE);
    }

    public SResponse<List<RootGroupSelectVo>> orderAllotSelectList() {
        List<RootGroupEntity> allList = rootGroupService.list(
                new QueryWrapper<RootGroupEntity>()
                        .eq("deleted", false)
                        .eq("status", true)
        );

        if (CollectionUtil.isEmpty(allList)){
            SResponse.fail(Constant.ERRCODE_LOGIC, "未查询到有效刷机组信息");
        }

        Map<Integer, List<RootGroupEntity>> groupMap = allList.stream().collect(Collectors.groupingBy(RootGroupEntity::getType));

        return SResponse.ok(RootGroupSelectVo.createList(groupMap));
    }

    private void checkGroupName(String name) {
        List<RootGroupEntity> list = rootGroupService.list(
                new QueryWrapper<RootGroupEntity>()
                        .eq("deleted", false)
                        .eq("name", name)
        );
        if (CollectionUtil.isNotEmpty(list)){
            throw new OutletOrderException("刷机组名已存在");
        }
    }
}
